﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Collab.Common;
using EnvDTE80;

namespace Collab.Modules.Session
{
    public class SolutionBuilder : SolutionTracker
    {
        private string folder;

        public SolutionBuilder(DTE2 dte) : base(dte) { }


        public void BuildSolution(Collab.Services.Remote.Entities.Solution solution, Action<IDictionary<string, string>, Action> requestFiles)
        {
            if (requestFiles == null)
                throw new ArgumentNullException("requestFile");

            if (solution == null)
                return;

            if (this.dte.Solution.IsOpen)
            {
                VerifySolution(solution);
            }
            else
            {
                SelectSolutionFolder(solution, requestFiles);
            }


        }

        private void SelectSolutionFolder(Collab.Services.Remote.Entities.Solution solution, Action<IDictionary<string, string>, Action> requestFiles)
        {
            System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
            DialogResult result = folderDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.folder = folderDialog.SelectedPath;
                Debug.WriteLine("Folder:" + this.folder + " Selected");

                IDictionary<string, string> fileRequests = new Dictionary<string, string>();
                if (!string.IsNullOrEmpty(System.IO.Path.Combine(this.folder, solution.Name)))
                {
                    fileRequests.Add(solution.Name, System.IO.Path.Combine(this.folder, solution.Name));
                    solution.Children.Run(i => RecursiveDownload(i, fileRequests));

                    requestFiles(fileRequests, () => this.dte.Solution.Open(System.IO.Path.Combine(this.folder, solution.Name)));
                }
            }
        }

        public void AddFile(Collab.Services.Remote.Entities.AddedFileInformation fileInformation, Action<IDictionary<string, string>, Guid, Action> requestFiles)
        {
            string absolutePath = System.IO.Path.Combine(this.SolutionPath, fileInformation.ProjectItem.RelativePath);

            var parent = FindProjectItemParentByParents(fileInformation.ProjectItem.Parents);

            this.projectItemsToIgnore.Add(absolutePath);
            if (fileInformation.ProjectItem.Kind == EnvDTEConstants.vsProjectItemKindPhysicalFolder)
            {

                if(parent is EnvDTE.Project){
                    (parent as EnvDTE.Project).ProjectItems.AddFolder(fileInformation.ProjectItem.Name);
                }else{
                    (parent as EnvDTE.ProjectItem).ProjectItems.AddFolder(fileInformation.ProjectItem.Name);
                }
            }
            else
            {
                IDictionary<string, string> fileRequests = new Dictionary<string, string>();
                fileRequests.Add(fileInformation.ProjectItem.RelativePath, absolutePath);

                requestFiles(fileRequests, fileInformation.UserId, () =>
                    {
                        if (parent is EnvDTE.Project)
                        {
                            (parent as EnvDTE.Project).ProjectItems.AddFromFile(absolutePath);
                        }
                        else
                        {
                            (parent as EnvDTE.ProjectItem).ProjectItems.AddFromFile(absolutePath);
                        }

                    }
                );
            }
        }


        private void VerifySolution(Services.Remote.Entities.Solution solution)
        {
            //TODO: Verfiy that the solutions are matching
        }


        private void RecursiveDownload(Collab.Services.Remote.Entities.ProjectItem item, IDictionary<string, string> fileRequests)
        {
            if (item.Kind == EnvDTEConstants.vsProjectItemKindPhysicalFolder)
            {
                Directory.CreateDirectory(System.IO.Path.Combine(this.folder, item.RelativePath));
                Debug.WriteLine("Created directory:" + System.IO.Path.Combine(this.folder, item.RelativePath));
                item.Children.Run(i => RecursiveDownload(i, fileRequests));
            }
            else if (!string.IsNullOrEmpty(item.RelativePath))//item.Kind != EnvDTEConstants.vsProjectItemKindSolutionItems && item.Kind != EnvDTEConstants.vsProjectKindSolutionItems)
            {
                fileRequests.Add(item.RelativePath, System.IO.Path.Combine(this.folder, item.RelativePath));
                item.Children.Run(i => RecursiveDownload(i, fileRequests));
            }
            else
            {
                item.Children.Run(i => RecursiveDownload(i, fileRequests));
            }
        }


    }
}
